﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Einstein.Data;

namespace Einstein.Group.DAL
{
    public class UserTypeInfoDAL
    {
        /// <summary>
        /// 获取所有的用户类型
        /// </summary>
        /// <returns></returns>
        public IList<UserTypeInfo> SelectAll()
        {
            using (EinsteinEntities entities = new EinsteinEntities())
            {
                return entities.UserTypeInfo.ToList<UserTypeInfo>();
            }
        }

        public bool Add(UserTypeInfo info)
        {
            using (EinsteinEntities entities = new EinsteinEntities())
            {
                entities.UserTypeInfo.AddObject(info);
                return entities.SaveChanges() > 0;
            }
        }

        /// <summary>
        /// 是否则存在相同的用户类型
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public bool Exist(UserTypeInfo info)
        {
            using (EinsteinEntities entities = new EinsteinEntities())
            {
                return entities.UserTypeInfo.Count(a => a.UserTypeName == info.UserTypeName && a.UserTypeID != info.UserTypeID) > 0;
            }
        }

        public bool Update(UserTypeInfo info)
        {

            using (EinsteinEntities entities = new EinsteinEntities())
            {
                foreach (var item in entities.UserTypeInfo.Where(a => a.UserTypeID == info.UserTypeID))
                {
                    item.UserTypeName = info.UserTypeName;
                    item.Desc = info.Desc;
                }
                return entities.SaveChanges() > 0;
            }
        }

        public bool Delete(UserTypeInfo info)
        {
            using (EinsteinEntities context = new EinsteinEntities())
            {
                foreach (var item in context.UserTypeInfo.Where(a => a.UserTypeID == info.UserTypeID))
                { 
                    context.DeleteObject(item);
                }
                return  context.SaveChanges()>0;
            }
        }
    }
}
